package org.toools.gwt.wmc.jpa;

import java.io.Serializable;
import java.util.Collection;
import java.util.Vector;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author stefan asseg
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "CompanyPart.getAll", query = "SELECT cp FROM CompanyPart cp"),
    @NamedQuery(name = "CompanyPart.getById", query = "SELECT cp FROM CompanyPart cp WHERE cp.id = :id"),
    @NamedQuery(name = "CompanyPart.getByName", query = "SELECT cp FROM CompanyPart cp WHERE cp.name = :name"),
    @NamedQuery(name = "CompanyPart.truncate", query = "DELETE FROM CompanyPart")
})
public class CompanyPart implements Serializable, com.google.gwt.user.client.rpc.IsSerializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;
    private String name;
    private int cpSize;
    @ManyToOne(cascade = CascadeType.PERSIST)
    private Company company;
    @ManyToMany(cascade = CascadeType.PERSIST, fetch=FetchType.EAGER)
    private Collection<Additive> additives = new Vector<Additive>();
    @ManyToMany(cascade = CascadeType.PERSIST, fetch=FetchType.EAGER)
    private Collection<Application> applications = new Vector<Application>();
    @ManyToMany(cascade = CascadeType.PERSIST, fetch=FetchType.EAGER)
    private Collection<Container> containers = new Vector<Container>();

    public CompanyPart() {
    }

    public CompanyPart(String name, int cpSize, Company company) {
        this.name = name;
        this.cpSize = cpSize;
        this.company = company;
    }

    public int getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return cpSize;
    }

    public void setSize(int cpSize) {
        this.cpSize = cpSize;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Collection<Additive> getAdditives() {
        return additives;
    }

    public void setAdditives(Collection<Additive> additives) {
        this.additives = additives;
    }

    public Collection<Application> getApplications() {
        return applications;
    }

    public void setApplications(Collection<Application> applications) {
        this.applications = applications;
    }

    public Collection<Container> getContainers() {
        return containers;
    }

    public void setContainers(Collection<Container> containers) {
        this.containers = containers;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CompanyPart)) {
            return false;
        }
        CompanyPart other = (CompanyPart) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String result = "CompanyPart [id = " + id + "] [name = " + name + "] [size = " + cpSize + "] [company = " + company + "] [applications = <";
        for (Application a : applications) {
            result += a;
        }
        result += ">] [additives = <";
        for (Additive a : additives) {
            result += a;
        }
        return result + ">]";
    }
}
